﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IP_PROJECT
{
    public partial class Article : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Submit_Click(object sender, EventArgs e)
        {
            String articleId = Request.QueryString["Id"];
            if (articleId != null)
            {
                DataClassesDataContext db = new DataClassesDataContext();
                Comment comment = new Comment();
                comment.Content = ((TextBox)LoginView.FindControl("CommentInput")).Text;
                comment.Nickname = User.Identity.Name;
                comment.Article = int.Parse(articleId);
                db.Comments.InsertOnSubmit(comment);
                db.SubmitChanges();
            }
            Response.Redirect(Request.Url.ToString());
        }

        protected void DeleteComment_Click(object sender, EventArgs e)
        {
            String id = ((Button)sender).CommandArgument;
            DataClassesDataContext db = new DataClassesDataContext();
            var result = from u in db.Comments
                         where u.Id == int.Parse(id)
                         select u;
            Comment comment = result.First();
            db.Comments.DeleteOnSubmit(comment);
            db.SubmitChanges();
            Response.Redirect(Request.Url.ToString());
        }

        protected void SaveArticle_Click(object sender, EventArgs e)
        {
            RepeaterItem repeater = ArticleRepeater.Items[0];
            LoginView loginView = repeater.FindControl("EditView") as LoginView;
            TextBox articleTitleBox = loginView.FindControl("ArticleTitle") as TextBox;
            TextBox articleDescrBox = loginView.FindControl("ArticleDescription") as TextBox;
            TextBox articleContentBox = loginView.FindControl("ArticleContent") as TextBox;
            DataClassesDataContext db = new DataClassesDataContext();
            String id = ((Button)sender).CommandArgument;
            var result = from u in db.Articles
                         where u.Id == int.Parse(id)
                         select u;
            Article article = result.First();
            article.Title = articleTitleBox.Text;
            article.Description = articleDescrBox.Text;
            article.Content = articleContentBox.Text;
            db.SubmitChanges();
            Response.Redirect("~/");
        }

        protected void DeleteArticle_Click(object sender, EventArgs e)
        {
            String id = ((Button)sender).CommandArgument;
            DataClassesDataContext db = new DataClassesDataContext();
            var result = from u in db.Articles
                         where u.Id == int.Parse(id)
                         select u;
            Article article = result.First();
            db.Articles.DeleteOnSubmit(article);
            db.SubmitChanges();
            Response.Redirect("~/");
        }
    }
}